package com.to.aboomy.bannersample.slice;

import com.to.aboomy.banner.Banner;
import com.to.aboomy.banner.BannerViewPager;
import com.to.aboomy.banner.IndicatorView;
import com.to.aboomy.banner.OnPageItemClickListener;
import com.to.aboomy.bannersample.ResourceTable;
import com.to.aboomy.bannersample.pagetransformer.AccordionTransformer;
import com.to.aboomy.bannersample.pagetransformer.AlphaPageTransformer;
import com.to.aboomy.bannersample.pagetransformer.BackgroundToForegroundTransformer;
import com.to.aboomy.bannersample.pagetransformer.DepthPageTransformer;
import com.to.aboomy.bannersample.pagetransformer.NoPageTransformer;
import com.to.aboomy.bannersample.pagetransformer.RotateDownPageTransformer;
import com.to.aboomy.bannersample.pagetransformer.RotateUpPageTransformer;
import com.to.aboomy.bannersample.pagetransformer.ZoomOutSlideTransformer;
import com.to.aboomy.bannersample.pagetransformer.ForegroundToBackgroundTransformer;
import com.to.aboomy.bannersample.pagetransformer.RotateDownTransformer;
import com.to.aboomy.bannersample.pagetransformer.RotateUpTransformer;
import com.to.aboomy.bannersample.pagetransformer.ScaleInOutTransformer;
import com.to.aboomy.bannersample.pagetransformer.ThreeScaleInTransformer;
import com.to.aboomy.bannersample.util.ArrayStringItemSelectDialog;
import com.to.aboomy.bannersample.util.ToastUtil;
import com.to.aboomy.bannersample.util.Utils;
import com.to.aboomy.bannersample.viewpager.creator.ImageHolderCreator2;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.AttrHelper;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.utils.Color;

import java.util.Arrays;
import java.util.List;

/**
 * 切换动画示例
 *
 * @since 2021-08-02
 */
public class AnimAbilitySlice extends AbilitySlice implements Component.ClickedListener {
    private static final int NUM5 = 5;
    private static final int NUM50 = 50;
    private static final int NUM30 = 30;
    private static final int NUMF1 = -1;
    private static final String[] ANIMS = {
        "AccordionTransformer",
        "BackgroundToForegroundTransformer",
        "ForegroundToBackgroundTransformer",
        "CubeInTransformer",
        "CubeOutTransformer",
        "DepthPageTransformer",
        "FlipHorizontalTransformer",
        "FlipVerticalTransformer",
        "RotateDownTransformer",
        "RotateUpTransformer",
        "ScaleInOutTransformer",
        "StackTransformer",
        "TabletTransformer",
        "ZoomInTransformer",
        "ZoomOutTransformer",
        "ZoomOutSlideTransformer",
    };

    private static final BannerViewPager.PageTransformer[] TRANSFORMERS = {
        new AccordionTransformer(),
        new BackgroundToForegroundTransformer(),
        new ForegroundToBackgroundTransformer(),
        null,// CubeInTransformer
        null,// CubeOutTransformer
        new DepthPageTransformer(),
        null,// FlipHorizontalTransformer
        null,// FlipVerticalTransformer
        new RotateDownTransformer(),
        new RotateUpTransformer(),
        new ScaleInOutTransformer(),
        null,// StackTransformer
        null,// TabletTransformer
        null,// new ZoomInTransformer()
        null,// new ZoomOutTransformer()
        new ZoomOutSlideTransformer(),
    };

    private static final String[] ANIMS2 = {
        "NonPageTransformer",
        "AlphaPageTransformer",
        "RotateDownPageTransformer",
        "RotateUpPageTransformer",
        "RotateYTransformer",
        "ScaleInTransformer",
    };

    private static final BannerViewPager.PageTransformer[] TRANSFORMERS2 = {
        new NoPageTransformer(),
        new AlphaPageTransformer(),
        new RotateDownPageTransformer(),
        new RotateUpPageTransformer(),
        null,
        new ThreeScaleInTransformer(),
    };
    private int initChoose1 = NUMF1;
    private int initChoose2 = NUMF1;
    private Banner banner;
    private Banner banner2;
    private Button selectAnim;
    private Button selectAnim2;
    private List<Integer> imageBanner1;
    private List<Integer> imageBanner2;

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_anim);
        banner = (Banner) findComponentById(ResourceTable.Id_banner);
        banner2 = (Banner) findComponentById(ResourceTable.Id_banner2);
        selectAnim = (Button) findComponentById(ResourceTable.Id_selectAnim);
        selectAnim2 = (Button) findComponentById(ResourceTable.Id_selectAnim2);
        selectAnim.setClickedListener(this);
        selectAnim2.setClickedListener(this);
        initBanner();
        initBanner2();
    }

    /**
     * initBanner
     */
    private void initBanner() {
        imageBanner1 = Utils.getImage(NUM5);

        banner.setHolderCreator(new ImageHolderCreator2())
                .setIndicator(new IndicatorView(this)
                        .setIndicatorStyle(IndicatorView.INDICATOR_CIRCLE)
                        .setIndicatorColor(Color.GRAY)
                        .setIndicatorSelectorColor(Color.WHITE)
                )
                .setPageTransformer(true, new NoPageTransformer())
                .setPages(imageBanner1);
        banner.setAutoPlay(true);

        banner.setOnPageClickListener(new OnPageItemClickListener() {
            @Override
            public void onPageItemClick(int position) {
                showToast("" + position);
            }
        });
    }

    /**
     * initBanner2
     */
    private void initBanner2() {
        imageBanner2 = Utils.getImage(NUM5);
        banner2.setHolderCreator(new ImageHolderCreator2())
                .setIndicator(new IndicatorView(this)
                        .setIndicatorStyle(IndicatorView.INDICATOR_CIRCLE)
                        .setIndicatorColor(Color.GRAY)
                        .setIndicatorSelectorColor(Color.WHITE))
                .setPageMargin(-(AttrHelper.vp2px(NUM50, getContext())), AttrHelper.vp2px(NUM30, getContext()))
                .setPageTransformer(true, new NoPageTransformer())
                .setPages(imageBanner2);
        banner2.setAutoPlay(true);

        banner2.setOnPageClickListener(new OnPageItemClickListener() {
            @Override
            public void onPageItemClick(int position) {
                showToast("" + position);
            }
        });
    }

    @Override
    public void onClick(Component component) {
        switch (component.getId()) {
            case ResourceTable.Id_selectAnim:
                new ArrayStringItemSelectDialog(getAbility(), Arrays.asList(ANIMS))
                        .setChoose(initChoose1)
                        .setOnItemClickListener((position, value) -> {
                            initChoose1 = position;
                            selectAnim.setText(value);
                            if (TRANSFORMERS[position] != null) {
                                banner.setPageTransformer(true, TRANSFORMERS[position]);
                                banner.setPages(imageBanner1);
                                banner.setAutoPlay(true);
                            } else {
                                showToast(value + "动画暂不支持");
                            }
                        }).show();
                break;
            case ResourceTable.Id_selectAnim2:
                new ArrayStringItemSelectDialog(getAbility(), Arrays.asList(ANIMS2))
                        .setChoose(initChoose2)
                        .setOnItemClickListener((position, value) -> {
                            initChoose2 = position;
                            selectAnim2.setText(value);
                            if (TRANSFORMERS2[position] != null) {
                                banner2.setPageTransformer(true, TRANSFORMERS2[position]);
                                banner2.setPages(imageBanner2);
                                banner2.setAutoPlay(true);
                            } else {
                                showToast(value + "动画暂不支持");
                            }
                        }).show();
                break;

            default:
                break;
        }
    }

    /**
     * showToast
     *
     * @param toastValue String
     */
    private void showToast(String toastValue) {
        ToastUtil.showToast(getContext(), toastValue);
    }

    @Override
    protected void onStop() {
        super.onStop();
    }
}
